package com.longcai.piaojia.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.longcai.piaojia.BaseApplication;
import com.longcai.piaojia.R;
import com.longcai.piaojia.conn.AddAddressPost;
import com.longcai.piaojia.dialog.ChooseAreaDialog;
import com.longcai.piaojia.util.MyToast;
import com.longcai.piaojia.util.Validator;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilMatches;

/**
 * Created by Administrator on 2016/8/5.
 */
public class MyReceiveAddressAddActivity extends BaseActivity implements View.OnClickListener{

    @BoundView(R.id.myreceive_address_add_name_et) private EditText myreceive_address_add_name_et;
    @BoundView(R.id.myreceive_address_add_phonenum_et) private EditText myreceive_address_add_phonenum_et;
    @BoundView(R.id.myreceive_address_add_address_et) private EditText myreceive_address_add_address_et;
    @BoundView(R.id.myreceive_address_add_idcard_et) private EditText myreceive_address_add_idcard_et;
    @BoundView(R.id.myreceive_address_add_area_tv) private TextView myreceive_address_add_area_tv;
    @BoundView(R.id.myreceive_address_add_saveaddress_tv) private TextView myreceive_address_add_saveaddress_tv;

    private boolean isChoose = false;

    private String mAreaId;

    private AddAddressPost addAddressPost = new AddAddressPost(new AsyCallBack<String>(){
        @Override
        public void onSuccess(String toast, int type, String s) throws Exception {

            Toast.makeText(MyReceiveAddressAddActivity.this, toast, Toast.LENGTH_LONG).show();

            try {

                ((MyReceiveAddressActivity.refreshAddressCallback) getAppCallBack(MyReceiveAddressActivity.class)).refreshAddress();

            } catch (NullPointerException e){

                e.printStackTrace();

            }

            if(isChoose){

                Intent intent = new Intent(MyReceiveAddressAddActivity.this, DispatchingTypeActivity.class);

                Bundle bundle = new Bundle();

                bundle.putString("name", myreceive_address_add_name_et.getText().toString().trim().replaceAll(" ",""));
                bundle.putString("address", myreceive_address_add_area_tv.getText().toString() + getString(R.string.space) + myreceive_address_add_address_et.getText().toString().trim().replaceAll(" ",""));
                bundle.putString("phone", myreceive_address_add_phonenum_et.getText().toString().trim().replaceAll(" ",""));

                intent.putExtras(bundle);

                MyReceiveAddressAddActivity.this.setResult(5, intent);

            }

            finish();
        }

        @Override
        public void onFail(String toast, int type) throws Exception {
            super.onFail(toast, type);

            Toast.makeText(MyReceiveAddressAddActivity.this, toast, Toast.LENGTH_LONG).show();

        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_myreceive_address_add);

        isChoose = getIntent().getBooleanExtra("isChoose", false);

        setBackTrue();

        setTitleName(getResources().getString(R.string.myreceive_address_add_title_text));

        myreceive_address_add_area_tv.setOnClickListener(this);
        myreceive_address_add_saveaddress_tv.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {

        switch (view.getId()){
            case R.id.myreceive_address_add_area_tv:

                //添加收货地址dialog

                new ChooseAreaDialog(context) {


                    @Override
                    public void onConfirm(String province, String provinceId, String city, String cityId, String area, String areaId) {


                        if (TextUtils.isEmpty(areaId)){

                            myreceive_address_add_area_tv.setText(province + getResources().getString(R.string.space) + city);

                            mAreaId = cityId;

                        } else {

                            myreceive_address_add_area_tv.setText(province + getResources().getString(R.string.space) + city + getResources().getString(R.string.space) + area);

                            mAreaId = areaId;
                        }

                    }
                }.show();

                break;

            case R.id.myreceive_address_add_saveaddress_tv:


                String nameStr = myreceive_address_add_name_et.getText().toString().trim().replaceAll(" ","");
                String phoneStr = myreceive_address_add_phonenum_et.getText().toString().trim().replaceAll(" ","");
                String addressStr = myreceive_address_add_address_et.getText().toString().trim().replaceAll(" ","");
                String idCardStr = myreceive_address_add_idcard_et.getText().toString().trim().replaceAll(" ","");

                if (TextUtils.isEmpty(nameStr)) {

                    MyToast.show(context, getString(R.string.myreceive_address_add_name_hint), Toast.LENGTH_LONG);

                    return;
                } else if (TextUtils.isEmpty(phoneStr)) {

                    MyToast.show(context, getString(R.string.myreceive_address_add_phonenum_hint), Toast.LENGTH_LONG);

                    return;
                } else if (!UtilMatches.checkMobile(phoneStr)) {

                    MyToast.show(context, getString(R.string.warning_please_fill_true_phone_text), Toast.LENGTH_LONG);

                    return;
                }  else if (TextUtils.isEmpty(mAreaId)){

                    MyToast.show(context, getString(R.string.warning_please_receive_address_area_text), Toast.LENGTH_LONG);

                    return;
                } else if (TextUtils.isEmpty(addressStr)) {

                    MyToast.show(context, getString(R.string.myreceive_address_add_address_hint), Toast.LENGTH_LONG);

                    return;
                }

//                Log.e("----------->", !Validator.isIDCard(idCardStr) + idCardStr);

                if (!TextUtils.isEmpty(idCardStr) && !Validator.isIDCard(idCardStr)) {

                    MyToast.show(context, getString(R.string.warning_please_fill_true_idnum_text), Toast.LENGTH_LONG);

                    return;
                }

                addAddressPost.user_id = BaseApplication.BasePreferences.readUserId();
                addAddressPost.address = addressStr;
                addAddressPost.idnumber = idCardStr;
                addAddressPost.name = nameStr;
                addAddressPost.phone = phoneStr;
                addAddressPost.region_id = mAreaId;

                addAddressPost.execute(MyReceiveAddressAddActivity.this);

                break;
        }

    }



}
